********************************************************************
*This file cleans enrollment in institutions in fall from the IPEDS*
********************************************************************

*** create dta files from csv, labeling
cd "$raw_data_education/fallEnrollment"

foreach i in 1990 1991 1992 1993 1994 1995 2000 2001 {
cap do "$do_clean_education/IPEDS - do/do fallEnrollment/ef`i'_a.do"
keep unitid line section lstudy efrace03 efrace04 efrace09 efrace10 efrace11 efrace12 efrace15 efrace16
rename efrace03 blackMen
rename efrace04 blackWomen
egen black = rsum(blackMen blackWomen), missing
label variable black "Black, non-Hispanic total"
rename efrace09 hispanicMen
rename efrace10 hispanicWomen
egen hispanic = rsum(hispanicMen hispanicWomen), missing
label variable hispanic "Hispanic total"
rename efrace11 whiteMen
rename efrace12 whiteWomen
egen white = rsum(whiteMen whiteWomen), missing
label variable white "white total"
rename efrace15 men
rename efrace16 women
egen total = rsum(men women), missing
label variable total "Grand total"
gen year = `i'
save "$clean_data_education/fallEnrollmentRace_`i'.dta", replace
clear
}
************************************************************

foreach i in 1999 2006 2007 {
cap do "$do_clean_education/IPEDS - do/do fallEnrollment/ef`i'anr.do"
keep unitid efalevel line section lstudy efrace03 efrace04 efrace09 efrace10 efrace11 efrace12 ///
efrace15 efrace16 efrace18 efrace21 efrace22 efrace24
rename efrace03 blackMen
rename efrace04 blackWomen
rename efrace18 black
rename efrace09 hispanicMen
rename efrace10 hispanicWomen
rename efrace21 hispanic
rename efrace11 whiteMen
rename efrace12 whiteWomen
rename efrace22 white
rename efrace15 men
rename efrace16 women
rename efrace24 total
gen year = `i'
save "$clean_data_education/fallEnrollmentRace_`i'.dta", replace
clear
}
************************************************************

foreach i in 2008 {
cap do "$do_clean_education/IPEDS - do/do fallEnrollment/ef`i'a.do"
keep unitid efalevel line section lstudy eftotlt eftotlm eftotlw dvefbkm dvefbkt dvefbkw dvefhst dvefhsm dvefhsw dvefwht dvefwhm dvefwhw
rename dvefbkm blackMen
rename dvefbkw blackWomen
rename dvefbkt black 
rename dvefhsm hispanicMen
rename dvefhsw hispanicWomen
rename dvefhst hispanic 
rename dvefwhm whiteMen
rename dvefwhw whiteWomen
rename dvefwht white
rename eftotlm men
rename eftotlw women
rename eftotlt total
gen year = `i'
save "$clean_data_education/fallEnrollmentRace_`i'.dta", replace
clear
}
************************************************************

foreach i in 2013 2014 2015 {
cap do "$do_clean_education/IPEDS - do/do fallEnrollment/ef`i'a.do"
keep unitid efalevel line section lstudy eftotlt eftotlm eftotlw efwhitt efwhitm efwhitw efhispt efhispm efhispw efbkaat efbkaam efbkaaw
rename efbkaam blackMen
rename efbkaaw blackWomen
rename efbkaat black 
rename efhispm hispanicMen
rename efhispw hispanicWomen
rename efhispt hispanic 
rename efwhitm whiteMen
rename efwhitw whiteWomen
rename efwhitt white
rename eftotlm men
rename eftotlw women
rename eftotlt total
gen year = `i'
save "$clean_data_education/fallEnrollmentRace_`i'.dta", replace
clear
}
************************************************************

* append
use "$clean_data_education/fallEnrollmentRace_1990.dta", clear
foreach i in 1991 1992 1993 1994 1995 1999 2000 2001 2006 2007 2008 2013 2014 2015 {
	append using "$clean_data_education/fallEnrollmentRace_`i'.dta", force
	rm "$clean_data_education/fallEnrollmentRace_`i'.dta"
	}
rm "$clean_data_education/fallEnrollmentRace_1990.dta"
	
* final cleanings
do "$do_clean_education/clean institutions - id.do"

* NO COLLAPSE HERE: this data must be reshaped 

	preserve 
	keep unitid year efalevel line section lstudy total men women
	save "$clean_data_education/fallEnrollmentTotals.dta", replace
	restore 


drop total men women
save "$clean_data_education/fallEnrollmentByRace.dta", replace

